package com.ordos.recruit.wrapper;

import com.wonders.commons.util.constant.BaseConstant;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;

/**
 * 读取流当中的数据存下来，然后获取流的时候，将存下来的数据重新转换成流输出出去
 *
 * @author jk
 * @date 2022/9/2 10:19
 */
public class RequestHeaderWrapper extends HttpServletRequestWrapper
{
    private final Map<String, String> headerMap = new HashMap<>();

    public RequestHeaderWrapper(HttpServletRequest request)
    {
        super(request);
    }

    @Override
    public String getHeader(String name) {
        String headerValue;
        if (headerMap.containsKey(name)) {
            headerValue = headerMap.get(name);
        }else {
            headerValue = super.getHeader(name);
        }
        return headerValue;
    }

    /**
     * get the Header names
     */
    @Override
    public Enumeration<String> getHeaderNames() {
        List<String> names = Collections.list(super.getHeaderNames());
        names.addAll(headerMap.keySet());
        return Collections.enumeration(names);
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        List<String> values;
        if (headerMap.containsKey(name)) {
            values = Collections.singletonList(headerMap.get(name));
        }else {
            values = Collections.list(super.getHeaders(name));
        }
        return Collections.enumeration(values);
    }
}